Unit BCommonConstsGeneratorUnit;

{$mode objfpc}{$H+}

Interface

Uses
  Classes, SysUtils, BListsUnit, BCommonUnit, BGeneratorUnit, BPeaUnit;

Type

{ BCommonConstsGeneratorClass }

 BCommonConstsGeneratorClass = Class(BGeneratorClass)
  Private
    bPeas: BPeasList;
    Procedure InitializeUnitFile(Const aKind: BFileKind); Override;
  Public
    Procedure Update; override;
    Procedure SavePeaUnit; override;
    Constructor Build(Const aPeaPath: String; Const aPeas: BPeasList;
      Const aProjectName: String);
End;

Implementation

ResourceString
  rsConstsDir = 'Consts/';
  mskCommonConsts = 'b%sconstsunit.pas';
  rsCommonConsts = 'commonconsts.ppp';
  rsTemplatesDir = 'Templates/';

  mskConst = 'rs%0:s = ''%0:s'';';

  {$define ppkPlainPlural :=
    ppkStrings,
    ppkIntegers,
    ppkDoubles,
    ppkDateTimes,
    ppkBooleans
  }

  {$define ppkPlain :=
    ppkString,
    ppkInteger,
    ppkDouble,
    ppkDate,
    ppkDateTime,
    ppkTime,
    ppkBoolean
  }

{ BCommonConstsGeneratorClass }

Procedure BCommonConstsGeneratorClass.InitializeUnitFile(
  Const aKind: BFileKind);
Var
  aTemplate: String;
Begin
  aTemplate := rsTemplatesDir + rsCommonConsts;
  If FileExists(aTemplate) Then
    bUnitFile.LoadFromFile(aTemplate);
  bUnitFile.Text := Format(bUnitFile.Text,
    [{0}bProjectName]);
End;

Procedure BCommonConstsGeneratorClass.Update;
Var
  aString: String;
  i, j, aIndex: Integer;
  aBuffer: BStringsList;
Begin
  aBuffer := BStringsList.Build;
  aIndex := ClearSection('CommonConsts');
  For i := 0 To bPeas.Count - 1 Do
    For j := 0 To bPeas[i].Properties.Count - 1 Do
      Begin
        Case bPeas[i][j].Kind Of
          ppkPlain, ppkPea:
            aString := bPeas[i][j].Caption;
          ppkPlainPlural, ppkPeas, ppkHolders:
            aString := bPeas[i][j].PluralCaption;
          Else
            aString := '';
        End;
        If Not(aString = '') And (aBuffer.IndexOf(aString) = -1) Then
          aBuffer.Add(aString);
      End;
  For i := 0 To aBuffer.Count - 1 Do
    AddLine(mskConst, [aBuffer[i]], 2, aIndex);
  aBuffer.Burn;
End;

Procedure BCommonConstsGeneratorClass.SavePeaUnit;
Begin
  bUnitFile.SaveToFile(bPeasPath + rsConstsDir +
    Format(mskCommonConsts, [LowerCase(bProjectName)]));
End;

Constructor BCommonConstsGeneratorClass.Build(Const aPeaPath: String;
  Const aPeas: BPeasList; Const aProjectName: String);
Var
  aPeaModelUnit: String;
Begin
  Inherited Build(aPeaPath, aProjectName);
  bPeas := aPeas;

  aPeaModelUnit := bPeasPath + rsConstsDir + rsCommonConsts;
  If FileExists(aPeaModelUnit) Then
    bUnitFile.LoadFromFile(aPeaModelUnit)
  Else
    InitializeUnitFile(fkCommonConsts);
End;

End.

